/**
 * IndexCtrl.java   2017年4月14日 下午4:46:27 by xuebing
 * <p>
 * Copyright (c) 2010 - 2017 All rights reserved.
 */
package com.rongmai.web.ctrl;

import com.rongmai.db.dto.LabelDto;
import com.rongmai.db.model.BaseResult;
import com.rongmai.web.bo.LabelBo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@RestController
@RequestMapping("/label")
public class LabelCtrl {

    @Resource
    private LabelBo labelBo;

    @RequestMapping(value = "tree/all/{code}/{role}", method = RequestMethod.GET)
    public BaseResult<LabelDto> getLabelTree4Self(@PathVariable String code, @PathVariable String role) {
        LabelDto labelDto = labelBo.labelTree(code, true, role);
        return BaseResult.succ(labelDto);
    }

    @RequestMapping(value = "tree/{code}/{role}", method = RequestMethod.GET)
    public BaseResult<LabelDto> getLabelTree(@PathVariable String code, @PathVariable String role) {
        LabelDto labelDto = labelBo.labelTree(code, false, role);
        return BaseResult.succ(labelDto);
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BaseResult save(@RequestBody LabelDto labelDto) {
        if (labelDto == null || StringUtils.isEmpty(labelDto.getName())) {
            return BaseResult.fail("参数非法");
        }
        return BaseResult.succ(labelBo.save(labelDto));
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Integer id) {
        labelBo.delete(id);
        return BaseResult.succ();
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public BaseResult<LabelDto> get(Integer id) {
        LabelDto labelDto = labelBo.get(id);
        return BaseResult.succ(labelDto);
    }
}
